/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspTaskIdentity;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.TaskIdentity;
import com.ffcs.crmd.tsp.server.util.PublicMethodHelper;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @since 
 *
 */
public class QueryTaskIdentityProcessor implements INettyProcessor {
    /**
     * 日志
     */
    private final static Logger LOG = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 
     */
    private DataBaseHandler     dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public QueryTaskIdentityProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    /**
     * 功能说明:
     * 
     * @param ctx
     * @param request
     * @return
     */
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.debug("SERVER-NETTY: one request to query task identity is received that body=[{}]", requestXml);
            TspTaskIdentity tspJobIdentityInfoVO = (TspTaskIdentity) XmlUtil.fromXml(requestXml, TspTaskIdentity.class);
            RetVo<TspTaskIdentity> tspTaskIdentityRetVo = new RetVo<TspTaskIdentity>();
            TaskIdentity taskIdentityParam = new TaskIdentity();
            BeanUtils.applyIf(taskIdentityParam, tspJobIdentityInfoVO);
            
            if (PublicMethodHelper.isQueryPage(tspJobIdentityInfoVO.getPageNumber(), tspJobIdentityInfoVO.getPageSize())) {
                PageInfo<TaskIdentity> taskIdentityPageInfo = dataBaseHandler.getServiceSupport().queryTaskIdentityPage(taskIdentityParam);
                PageInfo<TspTaskIdentity> tspTaskIdentityPageInfo = new PageInfo<TspTaskIdentity>();
                List<TspTaskIdentity> tspTaskIdentityList = BeanUtils.copyList(taskIdentityPageInfo.getList(), TspTaskIdentity.class);
                BeanUtils.applyIf(tspTaskIdentityPageInfo, taskIdentityPageInfo);
                tspTaskIdentityPageInfo.setList(tspTaskIdentityList);
                tspTaskIdentityRetVo.setPageInfo(tspTaskIdentityPageInfo);
            } else if (tspJobIdentityInfoVO.getId() != null) {
                TspTaskIdentity tspTaskIdentity = new TspTaskIdentity();
                TaskIdentity taskIdentity = dataBaseHandler.getServiceSupport().getTaskIdentity(taskIdentityParam);
                BeanUtils.applyIf(tspTaskIdentity, taskIdentity);
                tspTaskIdentityRetVo.setObject(tspTaskIdentity);
            } else {
                List<TaskIdentity> taskIdentityList = dataBaseHandler.getServiceSupport().queryTaskIdentity(taskIdentityParam);
                List<TspTaskIdentity> tspTaskIdentityList = BeanUtils.copyList(taskIdentityList, TspTaskIdentity.class);
                tspTaskIdentityRetVo.setDataList(tspTaskIdentityList);
            }
            response.setBody(XmlUtil.toXml(tspTaskIdentityRetVo).getBytes());
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query task identity[{}] exception:", requestXml, e);
            String responseBody = String.format("query task identity:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
}
